﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 11.12.2014
 * Time: 16:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Data;
using System.Data.Linq;
using System.Linq;
namespace Invent
{
  /// <summary>
  /// Description of DeliveryEdit.
  /// </summary>

  public partial class DeliveryEdit : Form
  {    
    DatabaseInvent m_db;
    Guid m_DeliveryUID;
    bool m_new = false;
    DataRow getRow(){ return (from d in m_db.Delivery.AsEnumerable() where d.Field<Guid>("DeliveryUID")==m_DeliveryUID select d).Single();}
    void ShowSelectors()
    {
      DeliveryOrganization.Items.Clear(); 
	  foreach(String r in 
          from row in m_db.Delivery.AsEnumerable() 
          group row by row.Field<String>("DeliveryOrganization") into g
          select g.Key)
	  {
	    DeliveryOrganization.Items.Add(r);
	  }   	
    }
    void showFoto()
    {
      listView1.Items.Clear();
      listView1.LargeImageList = new ImageList();
      listView1.LargeImageList.ColorDepth = ColorDepth.Depth24Bit;
      listView1.LargeImageList.ImageSize = new Size(200,200/4*3);
      foreach(DataRow foto in from f in m_db.Foto.AsEnumerable() where !f.IsNull("DeliveryUID") && f.Field<Guid>("DeliveryUID")==m_DeliveryUID select f)
      {
        String key = foto.Field<Guid>("FotoUID").ToString();
        String fotoPath =  Path.Combine(m_db.Dir.FullName,foto.Field<String>("FileLink"));
        Image image1 = null;
        try
        {
          image1 = new Bitmap(fotoPath);
        }
        catch(System.ArgumentException)
        {
          image1 = new Bitmap(1,1);
        }
          
        listView1.LargeImageList.Images.Add(key,image1);
        var lvi = new ListViewItem(foto.Field<String>("FotoInfo"));
        lvi.ImageKey = key;
        lvi.Tag = foto.Field<Guid>("FotoUID");
        listView1.Items.Add(lvi);
      }
    }
    public DeliveryEdit(DatabaseInvent db,Guid DeliveryUID)
    {
		m_db = db;
		m_DeliveryUID = DeliveryUID;
		if (m_DeliveryUID == Guid.Empty)
		{
		  DataRow row = m_db.newDelivery("Наименование","Поставщик");
		  m_DeliveryUID = row.Field<Guid>("DeliveryUID");
		  m_new = true;
		}
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
      ShowSelectors();
      showFoto();
      m_db.control_read(this,getRow());

    }
    void Button1Click(object sender, EventArgs e)
    {
    	m_new = false;
    	m_db.control_write(this,getRow());

      Close();
    }
    void Button3Click(object sender, EventArgs e)
    {
      m_db.deleteDelivery(m_DeliveryUID);
      Close();
    }
    void Button2Click(object sender, EventArgs e)
    {
      if (m_new)
        m_db.deleteDelivery(m_DeliveryUID);// Удалить если новый не сохранили
      Close();

    }
    void DeliveryEditFormClosed(object sender, FormClosedEventArgs e)
    {
       if (m_new)
        m_db.deleteDelivery(m_DeliveryUID);// Удалить если новый не сохранили    
    }
    void ListView1DragEnter(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
        var fileList = (String[])e.Data.GetData(DataFormats.FileDrop);
        Boolean cf = false;
        for(Int32 i=0;i<fileList.Length;i++)
          if (m_db.checkLink(fileList[i]))
            cf = true;
        if (cf)
          e.Effect = DragDropEffects.Copy;
      }  
    }
    void ListView1DragDrop(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
        var fileList = (String[])e.Data.GetData(DataFormats.FileDrop);
        Boolean cf = false;
        for(Int32 i=0;i<fileList.Length;i++)
          if (m_db.checkLink(fileList[i]))
        {
          var f = m_db.newFoto(Guid.Empty,m_DeliveryUID,fileList[i],new FileInfo(fileList[i]).Name);
          showFoto();
        }
      }    
    }
    void ListView1DoubleClick(object sender, EventArgs e)
    {
      if (listView1.SelectedItems.Count>0)
      {
        var iFotoUID = (Guid)listView1.SelectedItems[0].Tag;
        DataRow foto = (from d in m_db.Foto.AsEnumerable() where d.Field<Guid>("FotoUID")==iFotoUID select d).Single();
        String fotoPath =  Path.Combine(m_db.Dir.FullName,foto.Field<String>("FileLink"));
        var pr = new System.Diagnostics.Process();
        pr.StartInfo.FileName = fotoPath;
        pr.Start();           
      }
    }
    void ListView1KeyUp(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Delete)
      {
        m_db.deleteFoto((Guid)listView1.SelectedItems[0].Tag);
        showFoto();
      }  
    }
    void ListView1AfterLabelEdit(object sender, LabelEditEventArgs e)
    {
      var iFotoUID = (Guid)listView1.Items[e.Item].Tag;
      DataRow foto = (from d in m_db.Foto.AsEnumerable() where d.Field<Guid>("FotoUID")==iFotoUID select d).Single();
      foto["FotoInfo"] = e.Label;  
    }

  }
}
